package com.example.minefieldkeepercloud.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.example.minefieldkeepercloud.R;
import com.example.minefieldkeepercloud.activity.dailypower.DailyPowerAc;
import com.example.minefieldkeepercloud.adapter.DateTypeSelectListviewAdapter;
import com.example.minefieldkeepercloud.bean.FactoryBean;
import com.example.minefieldkeepercloud.fragment.MinerChartFragment;
import com.example.minefieldkeepercloud.fragment.MinerHarvestFragment;
import com.example.minefieldkeepercloud.interfac.ApiHttpCallback;
import com.example.minefieldkeepercloud.interfac.OnSelectParameterListener;
import com.example.minefieldkeepercloud.interfac.WechatServer;
import com.example.minefieldkeepercloud.network.MyGson;
import com.example.minefieldkeepercloud.network.NetHelperNew;
import com.example.minefieldkeepercloud.util.NetWorkUtil;
import com.example.minefieldkeepercloud.util.ToastUtil;
import com.example.minefieldkeepercloud.view.lazyviewpager.LazyFragmentPagerAdapter;
import com.example.minefieldkeepercloud.view.lazyviewpager.LazyViewPager;
import com.example.minefieldkeepercloud.view.pop.DateTypeSelectPop;
import com.example.minefieldkeepercloud.view.pop.FactorySelectPop;
import com.example.minefieldkeepercloud.view.pop.TypeSelectPopupWindow;

import org.greenrobot.eventbus.EventBus;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 矿机算力统计
 */
public class FieldCalculationStatisticsActivity extends AppCompatActivity implements OnSelectParameterListener {

    @BindView(R.id.viewpager)
    LazyViewPager viewpager;
    @BindView(R.id.ll_type)
    LinearLayout ll_type;
    @BindView(R.id.tv_type)
    TextView tv_type;
    @BindView(R.id.tv_datetype)
    TextView tv_datetype;
    @BindView(R.id.tv_date)
    TextView tv_date;
    @BindView(R.id.tv_factory)
    TextView tv_factory;
    @BindView(R.id.view_line)
    View view_line;


    private TypeSelectPopupWindow typeSelectPopupWindow;

    private int factoryId;//矿场id
    private int datetype;//日期类型
    private String dateString = "";//日期



    private DateTypeSelectPop dateTypeSelectPop;

    private SimpleDateFormat ymd = new SimpleDateFormat("yyyy-MM-dd");
    private SimpleDateFormat ym = new SimpleDateFormat("yyyy-MM");
    private String ymdnow = "";
    private String ymnow = "";
    private TimePickerView ymdTimeDay;//年月日时间选择器
    private TimePickerView ymTimeDay;//年月时间选择器

    private FactorySelectPop factorySelectPop;

    private CustomLazyFragmentPagerAdapter customLazyFragmentPagerAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_field_calculation_statistics);
        ButterKnife.bind(this);

        initView();
        initPop();
        initPvTimeYear();


        getMineField();



    }

    /**
     * 初始化筛选pop
     */
    private void initPop() {
        dateTypeSelectPop = new DateTypeSelectPop(this, new DateTypeSelectPop.OnSelectListener() {
            @Override
            public void selectDateType(int i) {
                if (i == 0) {
                    tv_datetype.setText("按日统计");
                    dateString = ymdnow;
                    tv_date.setText(ymdnow);
                } else if (i == 1) {
                    tv_datetype.setText("按月统计");
                    dateString = ymnow;
                    tv_date.setText(ymnow);
                }
                datetype = i;
                fragmentDataNofight();
            }
        });
    }

    /**
     * 时间选择器
     */
    private void initPvTimeYear() {


        Calendar calendar = Calendar.getInstance();
//        calendar.add(Calendar.DATE, -1); //向前走一天
        Date date = calendar.getTime();
        ymdnow = ymd.format(date);
        ymnow = ym.format(date);
        dateString = ymdnow;
        tv_date.setText(ymdnow);

        //年月日时间选择器
        ymdTimeDay = new TimePickerView(this, TimePickerView.Type.YEAR_MONTH_DAY);
        //设置范围
        ymdTimeDay.setRange(2017, 2020);

        //设置是否循环滚动
        ymdTimeDay.setCyclic(false);
        ymdTimeDay.setCancelable(true);

        //选择后回调
        ymdTimeDay.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {

            @Override
            public void onTimeSelect(Date date) {
                tv_date.setText(ymd.format(date));
                dateString = ymd.format(date);
                fragmentDataNofight();
            }


        });
        //年月选择器
        ymTimeDay = new TimePickerView(this, TimePickerView.Type.YEAR_MONTH);
        //设置范围
        ymTimeDay.setRange(2017, 2020);

        //设置是否循环滚动
        ymTimeDay.setCyclic(false);
        ymTimeDay.setCancelable(true);

        //时间选择后回调
        ymTimeDay.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {

            @Override
            public void onTimeSelect(Date date) {
                tv_date.setText(ym.format(date));
                dateString = ym.format(date);
                fragmentDataNofight();
            }


        });
    }



    @OnClick({R.id.ll_type, R.id.title_left, R.id.ll_datetype, R.id.ll_date, R.id.ll_factory})
    public void onClick(View view) {
        switch (view.getId()) {

            case R.id.ll_type:
                //选择统计表或统计图
                typeSelectPopupWindow.showAsDropDown(ll_type, 10, 0);
                break;
            case R.id.title_left:
                //返回
                finish();
                break;
            case R.id.ll_datetype:
                //选择日期类型
                if (dateTypeSelectPop != null) {
                    dateTypeSelectPop.showAsDropDown(view_line, 0, 0);
                }
                break;
            case R.id.ll_date:
                //选择日期
                if (datetype == 0) {
                    ymdTimeDay.setTime(getFormatDate(tv_date.getText().toString()));
                    ymdTimeDay.show();
                } else if (datetype == 1) {
                    ymTimeDay.setTime(getFormatDate(tv_date.getText().toString()));
                    ymTimeDay.show();
                }
                break;
            case R.id.ll_factory:
                if (factorySelectPop != null)
                    factorySelectPop.showAsDropDown(view_line, 0, 0);
                else
                    ToastUtil.showToast(FieldCalculationStatisticsActivity.this, "没有可查询的矿场");
                break;
        }
    }

    /**
     * 获取矿场数据
     */
    private void getMineField() {
        if (!NetWorkUtil.check(this)) {
            return;
        }
        NetHelperNew.GetFactory(new ApiHttpCallback(FieldCalculationStatisticsActivity.this, 1, true) {
            @Override
            public void onSuccess(String data) {
                FactoryBean factoryBean = MyGson.getInstance().fromJson(data, FactoryBean.class);
                if (factoryBean.getType() == 1) {
                    if (factoryBean.getData().size() > 0) {
                        CreatePop(factoryBean);
                    } else {
                        ToastUtil.showToast(FieldCalculationStatisticsActivity.this, "没有可查询的矿场");
                    }

                } else {
                    ToastUtil.showToast(FieldCalculationStatisticsActivity.this, factoryBean.getContent());
                }
            }
        });


    }

    /**
     * 矿场选择
     *
     * @param dataBean
     */
    private void CreatePop(FactoryBean dataBean) {
        if (factoryId == 0) {
            factoryId = dataBean.getData().get(0).getID();
        }
        if (factorySelectPop == null) {
            factorySelectPop = new FactorySelectPop(FieldCalculationStatisticsActivity.this, dataBean, new FactorySelectPop.OnSelectListener() {
                @Override
                public void select(int id, String name) {
                    tv_factory.setText(name);
                    factoryId = id;

                    fragmentDataNofight();


                }
            });
        }
        ;

        tv_factory.setText(dataBean.getData().get(0).getFactoryName());
        fragmentDataNofight();

    }

    /**
     * 更新数据
     */
    private void fragmentDataNofight() {
        EventBus.getDefault().post("10");
    }

    private void initView() {
        customLazyFragmentPagerAdapter = new CustomLazyFragmentPagerAdapter(getSupportFragmentManager());
        viewpager.setAdapter(customLazyFragmentPagerAdapter);
        viewpager.setCurrentItem(0, false);


        typeSelectPopupWindow = new TypeSelectPopupWindow(1,this, new TypeSelectPopupWindow.OnSelectLayoutListener() {
            @Override
            public void selectTypeLayout(int i) {

                if (i == 1) {
                    tv_type.setText("统计表");
                    viewpager.setCurrentItem(1, false);

                } else if (i == 0) {
                    tv_type.setText("统计图");
                    viewpager.setCurrentItem(0, false);

                }

            }
        });
    }


    /**
     * 获取矿场id
     *
     * @return
     */
    @Override
    public int getFactoryId() {
        return factoryId;
    }

    /**
     * 日期类型
     *
     * @return
     */
    @Override
    public int getDateType() {
        return datetype;
    }

    /**
     * 日期
     *
     * @return
     */
    @Override
    public String getDate() {
        return dateString;
    }

    /**
     * 转化时间
     *
     * @param s
     * @return
     */
    private Date getFormatDate(String s) {
        try {
            return ymd.parse(s);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
    private static class CustomLazyFragmentPagerAdapter extends LazyFragmentPagerAdapter {

        private CustomLazyFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(ViewGroup container, int position) {
            if (position == 0) {
                return new MinerChartFragment();
            } else if (position == 1) {
                return new MinerHarvestFragment();

            }
            return null;
        }

        @Override
        public int getCount() {
            return 2;
        }

    }

}
